﻿/* AgileFx Version 2.0
 * An open-source framework for rapid development of applications and services using Microsoft.net
 * Developed by: AgileHead
 * Website: www.agilefx.org
 * This component is licensed under the terms of the Apache 2.0 License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Mvc.Html;

namespace AgileFx.MVC.Controls
{
    public static partial class HtmlUtil
    {
        public static IEnumerable<SelectListItem> GetSelectItems<T>(IEnumerable<T> items, Func<T, object> valueSelector,
            Func<T, object> displaySelector, object selectedValue)
        {
            return items.Select(x =>
                new SelectListItem
                {
                    Text = displaySelector(x).ToString(),
                    Value = valueSelector(x).ToString(),
                    Selected = ((selectedValue ?? "").ToString() == (valueSelector(x) ?? "").ToString())
                });
        }

        public static IEnumerable<SelectListItem> GetSelectItems<T>(IEnumerable<T> items, Func<T, object> valueSelector,
            Func<T, object> displaySelector, Func<T, bool> isSelected)
        {
            return items.Select(x =>
                new SelectListItem
                {
                    Text = displaySelector(x).ToString(),
                    Value = valueSelector(x).ToString(),
                    Selected = isSelected(x)
                });
        }

        public static IEnumerable<SelectListItem> GetSelectItems<T>(IEnumerable<T> items, Func<T, object> valueSelector,
            Func<T, object> displaySelector)
        {
            return items.Select(x =>
                new SelectListItem
                {
                    Text = displaySelector(x).ToString(),
                    Value = valueSelector(x).ToString()
                });
        }

        public static string OptionList<T>(IEnumerable<T> items, Func<T, object> displayMember, 
            Func<T, object> valueMember, string selectedValue)
        {
            return OptionList(items, null, displayMember, valueMember, selectedValue);
        }

        public static string OptionList<T>(IEnumerable<T> items, string emptyOptionLabel,
            Func<T, object> displayMember, Func<T, object> valueMember,
            string selectedValue)
        {
            Func<string, string, bool, string> optionText = (t, v, b) =>
                string.Format("<option value=\"{1}\"{2}>{0}</option>", t, v, b ? " selected=\"selected\"" : "");

            StringBuilder html = new StringBuilder();
            if (!emptyOptionLabel.Empty())
                html.Append(optionText(emptyOptionLabel, "", (String.IsNullOrEmpty(selectedValue) ? true : false)));

            foreach (var item in items)
                html.Append(optionText(displayMember(item).ToString(), valueMember(item).ToString(),
                    valueMember(item).ToString() == selectedValue));

            return html.ToString();
        }

    }
}
